
#include "NVD1115.h"



/*------------------------------------------------------------------
初始化

*/ 
void NVD1115::begin(uint8_t(*__set_DirA)(void), uint8_t(*__reset_DirA)(void), uint8_t(*__set_DirB)(void), uint8_t(*__reset_DirB)(void), uint8_t(*__set_PeriodA)(uint32_t period), \
                      uint8_t(*__set_PeriodB)(uint32_t period), uint8_t(*__set_PulseWidthA)(uint32_t width), uint8_t(*__set_PulseWidthB)(uint32_t width))
{
    _set_DirA        = __set_DirA;
    _reset_DirA      = __reset_DirA;
    _set_DirB        = __set_DirB;
    _reset_DirB      = __reset_DirB;
    _set_PeriodA     = __set_PeriodA;
    _set_PeriodB     = __set_PeriodB;
    _set_PulseWidthA = __set_PulseWidthA;
    _set_PulseWidthB = __set_PulseWidthB;
    //it is necessary to set under for setting FreqA,FreqB,PulseWidthA,PulseWidthB
    SetFrequence(0,1000);
    SetFrequence(1,1000);
    SetSpeed(0,0);
    SetSpeed(1,0);
}

/*------------------------------------------------------------------
设置方向CW CCW

*/ 
uint8_t  NVD1115::SetDirection(uint8_t port, uint8_t dir)
{
    if(port == PORT_A)
    {
        if (dir == 0)
        {
            if(_reset_DirA() == 1)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
        else if(dir == 1)
        {
            if(_set_DirA() == 1)
            {
                return 1;
            }
            else 
            {
                return 0;
            }
        }
    }
    else if(port == PORT_B)
    {
        if (dir == 0)
        {
            if(_reset_DirB() == 1)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
        else if(dir == 1)
        {
            if(_set_DirB() == 1)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
        else
        {
            return 0;
        }
    }
    return 0;
}
/*------------------------------------------------------------------
设置PWM频率

*/ 

uint8_t NVD1115::SetFrequence(uint8_t port, uint32_t freq)
{
    uint32_t period = 1000000/freq;  //us
    
    if(port == PORT_A)
    {
        if(_set_PeriodA(period) == 1)
        {
          FreqA = freq;
          return 1;
        }
        else
        {
          return 0; 
        }
    }
    else if(port == PORT_B)
    {
        if(_set_PeriodB(period) == 1)
        {
          FreqB = freq;
          return 1;  
        }
        else
        {
          return 0;  
        }
    }
    else
    {
        return 0;
    }
}

uint8_t NVD1115::SetSpeed(uint8_t port, uint32_t SpeedRatio)
{
    uint32_t PulseWidth  = 0;
    uint32_t period = 0;
    if(port == PORT_A)
    {
        period = 1000000/FreqA;
        PulseWidth = SpeedRatio * period / 10000;
        if(_set_PulseWidthA(PulseWidth) == 1)
        {
            PulseWidthA = PulseWidth;
            return 1;
        }
        else
        {
            return 0;
        }
    }
    else if(port == PORT_B)
    {
        period = 1000000/FreqB;
        PulseWidth = SpeedRatio * period / 10000;
        if(_set_PulseWidthB(PulseWidth) == 1)
        {
            PulseWidthB = PulseWidth;
            return 1;
        }
        else
        {
            return 0;
        }
    }
    else
    {
        return 0;
    }
}

